package com.kokotchy.tuxcontrol;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Attitune of the Tux Droid
 * 
 * $Date: 2009-12-30 15:09:35 +0000 (Wed, 30 Dec 2009) $
 * 
 * @author $Author: kokotchy $
 * @version $Revision: 18 $
 * 
 */
public class Attitune {

	/**
	 * TODO Fill it
	 */
	public static final int ATTITUNE_FILE = 1;

	/**
	 * TODO Fill it
	 */
	public static final int ATTITUNE_URL = 2;

	/**
	 * Author
	 */
	private String author;

	/**
	 * Category
	 */
	private String category;

	/**
	 * Description
	 */
	private String description;

	/**
	 * Keywords
	 */
	private String keywords;

	/**
	 * Language
	 */
	private String language;

	/**
	 * Length
	 */
	private float length;

	/**
	 * Name
	 */
	private String name;

	/**
	 * Sub Category
	 */
	private String subCategory;

	/**
	 * Version
	 */
	private String version;

	/**
	 * Return the author
	 * 
	 * @return the author
	 */
	public String getAuthor() {
		return author;
	}

	/**
	 * Return the category
	 * 
	 * @return the category
	 */
	public String getCategory() {
		return category;
	}

	/**
	 * Return the description
	 * 
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Return the keywords
	 * 
	 * @return the keywords
	 */
	public String getKeywords() {
		return keywords;
	}

	/**
	 * Return the language
	 * 
	 * @return the language
	 */
	public String getLanguage() {
		return language;
	}

	/**
	 * Return the length
	 * 
	 * @return the length
	 */
	public float getLength() {
		return length;
	}

	/**
	 * Return the name
	 * 
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Return the sub category
	 * 
	 * @return the subCategory
	 */
	public String getSubCategory() {
		return subCategory;
	}

	/**
	 * Return the version
	 * 
	 * @return the version
	 */
	public String getVersion() {
		return version;
	}

	/**
	 * Play the current attitune
	 */
	public void play() {
		play(0);
	}

	/**
	 * Play the current attitune from start
	 * 
	 * @param start
	 *            Begin of the attitune
	 */
	public void play(float start) {
		String pattern = "http://127.0.0.1:54321/%d/attitune_manager/start_attitune_by_name?begin=%f&name=%s";
		try {
			URL url = new URL(String.format(pattern, 0, start, name.replace(
					" ", "%20")));
			URLConnection connection = url.openConnection();
			connection.getInputStream();
			// BufferedReader bufferedReader = new BufferedReader(
			// new InputStreamReader(connection.getInputStream()));
			// String line = "";
			// while ((line = bufferedReader.readLine()) != null) {
			// System.out.println(line);
			// }
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Define the author
	 * 
	 * @param author
	 *            the author to set
	 */
	public void setAuthor(String author) {
		this.author = author;
	}

	/**
	 * Define the category
	 * 
	 * @param category
	 *            the category to set
	 */
	public void setCategory(String category) {
		this.category = category;
	}

	/**
	 * Define the description
	 * 
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Set the given field
	 * 
	 * @param name
	 *            Name of the field
	 * @param value
	 *            Value of the field
	 */
	public void setField(String name, String value) {
		if (name.equals("author")) {
			setAuthor(value);
		} else if (name.equals("category")) {
			setCategory(value);
		} else if (name.equals("description")) {
			setDescription(value);
		} else if (name.equals("keywords")) {
			setKeywords(value);
		} else if (name.equals("language")) {
			setLanguage(value);
		} else if (name.equals("length")) {
			setLength(Float.parseFloat(value));
		} else if (name.equals("name")) {
			setName(value);
		} else if (name.equals("sub_category")) {
			setSubCategory(value);
		} else if (name.equals("version")) {
			setVersion(value);
		}
	}

	/**
	 * Define the keywords
	 * 
	 * @param keywords
	 *            the keywords to set
	 */
	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	/**
	 * Define the language
	 * 
	 * @param language
	 *            the language to set
	 */
	public void setLanguage(String language) {
		this.language = language;
	}

	/**
	 * Define the length
	 * 
	 * @param length
	 *            the length to set
	 */
	public void setLength(float length) {
		this.length = length;
	}

	/**
	 * Define the name
	 * 
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Define the sub category
	 * 
	 * @param subCategory
	 *            the subCategory to set
	 */
	public void setSubCategory(String subCategory) {
		this.subCategory = subCategory;
	}

	/**
	 * Define the version
	 * 
	 * @param version
	 *            the version to set
	 */
	public void setVersion(String version) {
		this.version = version;
	}

	@Override
	public String toString() {
		return name;
	}
}
